// pages/index/temporary/strategy.js
//var postData = require('../../data/post-data.js')
var app = getApp();
var page = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,  
    // tab切换 
    currentTab: 10,
    scrollLeft: 0, //tab标题的滚动条位置
    clicked: -1,
    page: 1,
    cate_id: '0',
    show_up: "https://img.xtuan.com/xcximgtest/images/materal/shouqi.png",
    companyid: '',
    // ordinary: false,
    // therecom: true,
    load: 10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(opctions) {
    var that = this
    console.log(opctions)
    var companyid = opctions.id
    console.log(opctions.id)
    setTimeout(function() {
      that.getrecommed()
    }, 1000)
    that.getnavlist()
    //that.getlist()
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          // postlist: postData.poststrategy,
          // the_before: postData.the_before,
          // the_loading: postData.the_loading,
          // the_after: postData.the_after,
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      },
    });
    that.setData({
      companyid: companyid
    })
  },
  //导航头部列表
  getnavlist: function(e) {
    var that = this
    wx.request({
      url: app.apiUrl('c=zhuangxiugonglve/index&a=getconfig'),
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      success: function(res) {
        var polists = res.data.datas
        console.log(res.data)
        that.setData({
          polistnav: polists
        })
      },
      fail: function(res) {
        console.log('请求失败', res.data)
      }
    })
  },
  //推荐数据列表
  getrecommed: function(e) {
    var that = this
    var companyid = that.data.companyid
    wx.request({
      url: app.apiUrl('c=zhuangxiugonglve/index&a=get_company_news_list'),
      data: {
        company_id: companyid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      success: function(res) {
        var polists = res.data.datas
        console.log('推荐请求的数据为', res.data)
        console.log(polists.length)
        if (polists.length == 0) {
          that.getlist()
          that.setData({
            ordinary: true,
            therecom: false,
            singlenav:false,
            load: 103
          })
        } else {
          that.setData({
            ordinary: false,
            therecom: true,
            singlenav: true,
            postlist: polists
          })
        }
        that.setData({
          // polistnav: polists
        })
      },
      fail: function(res) {
        console.log('请求失败', res.data)
      }
    })
  },
  //普通数据列表
  getlist: function(e) {
    var that = this
    var id = that.data.companyid
    wx.request({
      url: app.apiUrl('c=zhuangxiugonglve/index&a=getlist'),
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      success: function(res) {
        var polist = res.data.datas
        console.log(res.data)
        that.setData({
          postlist: polist
        })
      },
      fail: function(res) {
        console.log('请求失败', res.data)
      }
    })
  },

  //头部下拉弹框导航选择
  choose_up: function(e) {
    var that = this
    var show_down = "https://img.xtuan.com/xcximgtest/images/materal/zhankai.png"
    that.setData({
      show_up: show_down,
      show_swiper: true,
      showMask: true
    })
  },
  //点击蒙版隐藏
  clickmask: function () {
    var that = this
    var show_down = "https://img.xtuan.com/xcximgtest/images/materal/shouqi.png"
    that.setData({
      show_up: show_down,
      show_swiper: false,
      showMask: false
    })
  },
  // 普通导航点击
  swichNav: function(e) {
    var that = this;
    var page = 1
    var id = e.target.dataset.current
    var chid = e.target.dataset.chid
    var show_down = "https://img.xtuan.com/xcximgtest/images/materal/shouqi.png"
    console.log(id)
    // if (this.data.clicked == e.currentTarget.id) {
    //   this.setData({
    //     clickId: +1,
    //   })
    //   return;
    // }
    if (id>105){
      var left = id-105
      that.setData({
        scrollLeft: left*80
      })
    }else{
      that.setData({
        scrollLeft: 0
      })
    }
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
        clicked: e.currentTarget.id,
        show_up: show_down,
        show_swiper: false,
        showMask: false
      })
    }
    wx.request({
      url: app.apiUrl('c=zhuangxiugonglve/index&a=getlist'),
      data: {
        cate_id: id,
        page: page
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      success: function(res) {
        var polist = res.data.datas
        console.log(res.data)
        that.setData({
          postlist: polist
        })
      },
      fail: function(res) {
        console.log('请求失败', res.data)
      }
    })
    that.setData({
      page: page,
      cate_id: id,
      ordinary: true,
      therecom: false,
      load: id
    })
    wx.pageScrollTo({
      scrollTop: 0
    })
  },
  //推荐点击
  recommed: function(e) {
    var that = this;
    var page = 1
    var id = e.target.dataset.current
    var companyid = that.data.companyid
    var show_down = "https://img.xtuan.com/xcximgtest/images/materal/shouqi.png"
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    wx.request({
      url: app.apiUrl('c=zhuangxiugonglve/index&a=get_company_news_list'),
      data: {
        cate_id: id,
        page: page,
        company_id: companyid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      success: function(res) {
        var polist = res.data.datas
        console.log(res.data)
        that.setData({
          postlist: polist
        })
      },
      fail: function(res) {
        console.log('请求失败', res.data)
      }
    })
    that.setData({
      ordinary: false,
      therecom: true,
      load: id,
      show_up: show_down,
      show_swiper: false,
      showMask: false
    })
    wx.pageScrollTo({
      scrollTop: 0
    })
  },
  // 滚动切换标签样式
  switchTab: function(e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function() {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  //普通导航到详情
  binddetail: function(e) {
    console.log(e)
    var arcleId = e.currentTarget.dataset.arcleid;
    var detail = 1
    wx.navigateTo({
      url: "../../detail/strategy_detail?id=" + [detail,arcleId]
    })
  },
  //推荐到详情
  recommeddetail: function(e) {
    var arcleId = e.currentTarget.dataset.arcleid;
    console.log(arcleId)
    var detail = 2
    wx.navigateTo({
      url: "../../detail/strategy_detail?id=" + [detail, arcleId]
    })
  },
  //上拉加载
  onReachBottom: function() {
    var that = this
    var id = that.data.cate_id
    var load = that.data.load
    var companyid = that.data.companyid
    that.setData({
      page: that.data.page + 1 //后台数据需要分页，每次加载一页
    })
    wx.showLoading({ //显示toast
      title: '加载中...',
    });
    var page = that.data.page
    if (load == 10) {
      wx.request({
        url: app.apiUrl('c=zhuangxiugonglve/index&a=get_company_news_list'),
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          cate_id: id,
          page: that.data.page,
          company_id: companyid
        },
        method: 'GET',
        success: function(res) {
          if (res.data.code == 200) {
            var polist = that.data.postlist
            for (var i = 0; i < res.data.datas.length; i++) {
              polist.push(res.data.datas[i]);
            }
            console.log(polist)
            that.setData({
              postlist: polist
            });
            //page++
          } else {
            wx.showLoading({
              title: '加载完毕',
            })
          }
          console.log(page)
        },
        fail: function() {
          // fail
        },
        complete: function() {
          // complete
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          wx.hideLoading(); //隐藏toast
        }
      })
    } else {
      wx.request({
        url: app.apiUrl('c=zhuangxiugonglve/index&a=getlist'),
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          cate_id: id,
          page: that.data.page
        },
        method: 'GET',
        success: function(res) {
          if (res.data.code == 200) {
            var polist = that.data.postlist
            for (var i = 0; i < res.data.datas.length; i++) {
              polist.push(res.data.datas[i]);
            }
            console.log(polist)
            that.setData({
              postlist: polist
            });
            //page++
          } else {
            wx.showLoading({
              title: '加载完毕',
            })
          }
          console.log(page)
        },
        fail: function() {
          // fail
        },
        complete: function() {
          // complete
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          wx.hideLoading(); //隐藏toast
        }
      })
    }

  },
  //下拉刷新
  onPullDownRefresh: function() {
    var that = this
    var page = that.data.page
    var areaID = that.data.areaID
    var decorationID = that.data.decorationID
    var styleID = that.data.styleID
    var id = that.data.cate_id
    var load = that.data.load
    var companyid = that.data.companyid
    // wx.showLoading({
    //   title: '数据加载中',
    // })
    if(load==10){
      wx.request({
        url: app.apiUrl('c=zhuangxiugonglve/index&a=get_company_news_list'),
        data: {
          cate_id: id,
          page: page,
          company_id: companyid
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: "GET",
        success: function (res) {
          var polist = res.data.datas
          console.log(res.data)
          that.setData({
            postlist: polist
          })
        },
        fail: function (res) {
          console.log('请求失败', res.data)
        },
        complete: function () {
          // complete
          setTimeout(function () {
            wx.hideLoading()
          }, 2000)
          wx.stopPullDownRefresh() //停止下拉刷新
        }
      })
    }else{
      wx.request({
        url: app.apiUrl('c=zhuangxiugonglve/index&a=getlist'),
        method: 'GET',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          cate_id: id,
          page: page
        },
        success: function (res) {
          var polist = res.data.datas
          that.setData({
            postlist: polist
          })
        },
        fail: function () {
          // fail
        },
        complete: function () {
          // complete
          setTimeout(function () {
            wx.hideLoading()
          }, 2000)
          wx.stopPullDownRefresh() //停止下拉刷新
        }
      })
    }
   
  },

})